package com.bzframework.pro.service;

import com.bzframework.pro.constant.CommonConstants;
import com.bzframework.pro.entity.CustomerEntity;
import com.bzframework.pro.model.school.SchoolHouseKeeperBatchResponse;
import com.bzframework.pro.model.school.SingleCustomerResponse;
import com.bzframework.pro.model.school.employee.EmployeeResponse;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;

/**
 * @Author bingzhong
 * @Date 2025/3/21 10:50
 * @Description
 */
@Service
public class CustomerService extends GenericReactiveCrudService<CustomerEntity, String> {


    /**
     * 校管家根据id获取员工信息
     * @param employeeId
     * @param accessToken
     * @return
     */
    public Mono<SingleCustomerResponse> fetchEmployeeData(String employeeId, String accessToken) {
        return WebClient
                .create(CommonConstants.SCHOOL_HOUSEKEEPER_PRODUCTION_URL)
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/org/get_employee")
                        .queryParam("access_token", accessToken)
                        .queryParam("employee_id", employeeId)
                        .build())
                .retrieve()
                .bodyToMono(SingleCustomerResponse.class)
                .timeout(Duration.ofSeconds(15)) // 单独请求超时
                .retryWhen(Retry.backoff(3, Duration.ofSeconds(1))); // 指数退避重试;;
    }


    public Mono<EmployeeResponse> fetchEmployeeDataBatch(String accessToken, int offset) {

        return WebClient
                .create(CommonConstants.SCHOOL_HOUSEKEEPER_PRODUCTION_URL)
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/org/get_employee_batch")
                        .queryParam("access_token", accessToken)
                        .queryParam("updatetime", "19700101")
                        .queryParam("offset", offset)
                        .queryParam("size", 100)
                        .build())
                .retrieve()
                .bodyToMono(EmployeeResponse.class)
                .timeout(Duration.ofSeconds(15)) // 单独请求超时
                .retryWhen(Retry.backoff(3, Duration.ofSeconds(1))); // 指数退避重试;;

    }


    public Mono<SchoolHouseKeeperBatchResponse> fetchCustomerDataBatch(int offset, String accessToken) {
        return WebClient
                .create(CommonConstants.SCHOOL_HOUSEKEEPER_PRODUCTION_URL)
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/customer/get_customer_batch")
                        .queryParam("access_token", accessToken)
                        .queryParam("inputDate", "19700101")
                        .queryParam("offset", offset)
                        .queryParam("size", "100")
                        .build())
                .accept(MediaType.APPLICATION_JSON) // 明确要求JSON响应
                .retrieve()
                .bodyToMono(SchoolHouseKeeperBatchResponse.class)
                .timeout(Duration.ofSeconds(50)) // 单独请求超时
                .retryWhen(Retry.backoff(3, Duration.ofSeconds(1))); // 指数退避重试;;;
    }






}
